package com.version1_3.controller.Download;

import com.util.property.Properties;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by kuibu on 16/1/27.
 */

@Controller
public class DownloadController {
    private static final Logger LOGGER = Logger.getLogger(DownloadController.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_3/download/{phoneType}
     *
     * @Description: 跬步官方下载App记录
     */
    @RequestMapping(value = "/version1_3/download/{phoneType}", method = RequestMethod.GET)
    public String downloadAPP(@PathVariable("phoneType") int phoneType) {
        String phoneOS = (phoneType == 0) ? "Android" : "IOS";
        String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());

        String sqlStr = "INSERT INTO app_downloadrecord(phoneOS,downloadDate) VALUES (?,?)";
        int row = jdbcTemplate.update(sqlStr, new Object[]{phoneOS, dateStr});
        if (row != 1) {
            LOGGER.error("下载跬步APP失败");
        }
        String redirectUrl = (phoneType == 0) ? Properties.KuibuApp_downloadLink_Android : Properties.KuibuApp_downloadLink_IOS;

        return "redirect:" + redirectUrl;
    }

    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_3/downloadFromPage/{phoneType}/{pageId}
     *
     * @Description: 跬步官方下载App记录
     */
    @RequestMapping(value = "/version1_3/downloadFromPage/{phoneType}/{pageId}", method = RequestMethod.GET)
    public String downloadFromPage(@PathVariable("phoneType") int phoneType, @PathVariable("pageId") int pageId) {
        String phoneOS = (phoneType == 0) ? "Android" : "IOS";
        String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());

        String sqlStr = "INSERT INTO app_downloadrecord(phoneOS,downloadDate,pageId) VALUES (?,?,?)";
        int row = jdbcTemplate.update(sqlStr, new Object[]{phoneOS, dateStr, pageId});
        if (row != 1) {
            LOGGER.error("下载跬步APP失败");
        }
        String redirectUrl = (phoneType == 0) ? Properties.KuibuApp_downloadLink_Android : Properties.KuibuApp_downloadLink_IOS;

        return "redirect:" + redirectUrl;
    }


    /**
     * 调用接口：http://www.kuibutech.com/kuibuversion1/version1_3/download/{phoneType}/{platform}
     *
     * @Description: 第三方平台下载App记录
     */
    @RequestMapping(value = "/version1_3/download/{phoneType}/{platform}", method = RequestMethod.GET)
    public String downloadAPPCooperation(@PathVariable("phoneType") int phoneType, @PathVariable("platform") String platform) {
        String phoneOS = (phoneType == 0) ? "Android" : "IOS";
        String dateStr = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());

        String sqlStr = "INSERT INTO app_downloadrecord(phoneOS,downloadDate,platform) VALUES (?,?,?)";
        int row = jdbcTemplate.update(sqlStr, new Object[]{phoneOS, dateStr, platform});
        if (row != 1) {
            LOGGER.error("下载跬步APP失败");
        }
        String redirectUrl = (phoneType == 0) ? Properties.KuibuApp_downloadLink_Android : Properties.KuibuApp_downloadLink_IOS;

        return "redirect:" + redirectUrl;
    }

}
